package com.md.api.controller;

import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.WxMiniLoginReq;
import lombok.extern.slf4j.Slf4j;
import com.md.api.service.WxService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RequestMapping("/wx/mini")
@RestController
public class WxMiniController {


	@Resource
	WxService wxService;

	@RequestMapping("/getOpenId")
	public HttpResult getOpenId(@RequestBody WxMiniLoginReq wxMiniLoginReq) {
		if (StringUtils.isEmpty(wxMiniLoginReq.getCode())||StringUtils.isEmpty(wxMiniLoginReq.getEncryptedData())||StringUtils.isEmpty(wxMiniLoginReq.getIvStr())){
			return HttpResult.PARAM_ERROR;
		}
		return wxService.getOpenId(wxMiniLoginReq);
	}

	/**
	 * 小程序一键登录
	 * @return openId
	 */
	@RequestMapping("/login")
	public HttpResult miniProgramLogin(@RequestBody WxMiniLoginReq wxMiniLoginReq) {
		if (StringUtils.isEmpty(wxMiniLoginReq.getCode())||StringUtils.isEmpty(wxMiniLoginReq.getEncryptedData())||StringUtils.isEmpty(wxMiniLoginReq.getIvStr())){
			return HttpResult.PARAM_ERROR;
		}
		return wxService.login(wxMiniLoginReq);
	}

}
